﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace 文件查找
{
    public partial class FolderBrowserWindow : Window
    {
        public string SelectedPath { get; private set; } = string.Empty;

        public FolderBrowserWindow()
        {
            InitializeComponent();
            LoadDrives();
        }

        private void LoadDrives()
        {
            var drives = new ObservableCollection<FolderItem>();
            foreach (var drive in DriveInfo.GetDrives())
            {
                drives.Add(new FolderItem { Name = drive.Name, Path = drive.RootDirectory.FullName });
            }
            FolderTreeView.ItemsSource = drives;
        }

        private void FolderTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is FolderItem item)
            {
                SelectedPath = item.Path;
                PathTextBox.Text = item.Path;
                LoadSubFolders(item);
            }
        }

        private void LoadSubFolders(FolderItem item)
        {
            try
            {
                item.SubFolders.Clear();
                foreach (var dir in Directory.GetDirectories(item.Path))
                {
                    item.SubFolders.Add(new FolderItem
                    {
                        Name = Path.GetFileName(dir),
                        Path = dir
                    });
                }
            }
            catch (UnauthorizedAccessException)
            {
                // 处理无权限访问的目录
            }
        }

        private void UpButton_Click(object sender, RoutedEventArgs e)
        {
            if (!string.IsNullOrEmpty(SelectedPath))
            {
                var parent = Directory.GetParent(SelectedPath);
                if (parent != null)
                {
                    SelectedPath = parent.FullName;
                    PathTextBox.Text = parent.FullName;
                    // 更新TreeView选中项
                    var items = FolderTreeView.ItemsSource as ObservableCollection<FolderItem>;
                    if (items != null)
                    {
                        SelectFolder(items, parent.FullName);
                    }
                }
            }
        }

        private void SelectFolder(ObservableCollection<FolderItem> items, string path)
        {
            foreach (var item in items)
            {
                if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
                {
                    var tvItem = (TreeViewItem)FolderTreeView.ItemContainerGenerator.ContainerFromItem(item);
                    tvItem.IsSelected = true;
                    return;
                }
                if (path.StartsWith(item.Path, StringComparison.OrdinalIgnoreCase))
                {
                    var tvItem = (TreeViewItem)FolderTreeView.ItemContainerGenerator.ContainerFromItem(item);
                    tvItem.IsExpanded = true;
                    tvItem.UpdateLayout();
                    SelectFolder(item.SubFolders, path);
                }
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }

    public class FolderItem
    {
        public string Name { get; set; } = string.Empty;
        public string Path { get; set; } = string.Empty;
        public ObservableCollection<FolderItem> SubFolders { get; set; } = new ObservableCollection<FolderItem>();
    }
}
